//
//  LiveDetailController.m
//  JZMCFM
//
//  Created by lanou3g on 15/11/3.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "LiveDetailController.h"
#import "ListDetalilViewCell.h"
#import "ListDetailModel.h"
#import "ListDetail1ViewCell.h"
#import "listDetail21ViewController.h"

@interface LiveDetailController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTableView;

//保存数据的数据
@property(nonatomic,strong) NSMutableArray *listMutArray;

@property(nonatomic,strong) NSArray *array;
@end

@implementation LiveDetailController

#pragma mark 懒加载
-(NSMutableArray*)listMutArray{
    
    if (!_listMutArray) {
        _listMutArray = [NSMutableArray array];
    }
    return _listMutArray;
}

-(NSArray*)array{
    return [self.listMutArray copy];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerNib:[UINib nibWithNibName:@"ListDetalilViewCell" bundle:nil] forCellReuseIdentifier:@"detail"];
    
    
    [self downLoadFromNetWotk];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld",self.array.count);
    return self.array.count;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

         ListDetailModel *model = self.array[indexPath.row];
        if (model.tracks < 1) {
            ListDetalilViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
            cell.Serial.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
            [cell.coverSmall sd_setImageWithURL:[NSURL URLWithString:model.coverSmall]];
                cell.title.text = model.title;
            cell.nickname.text = model.nickname;
            cell.updatedAt.text = [NSString stringWithFormat:@"%ld",model.updatedAt];
            return cell;
        }else{
            ListDetalilViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
            cell.Serial.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
            cell.nickname.text = model.tags;
            cell.title.text = model.title;
            [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
            return cell;
        }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}


#pragma mark cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    listDetail21ViewController *listVC = [[listDetail21ViewController alloc]init];
    //把id传过去
    ListDetailModel *model = self.array[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"%lid",(long)model.id];
    listVC.receive = string;
    [self.navigationController pushViewController:listVC animated:YES];
        

}



#pragma mark 解析数据
-(void)downLoadFromNetWotk{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *string1 = @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/";
        NSString *string2 = @"?device=android&key=";
        NSString *string3 = @"&pageId=1&pageSize=40";
        
        string1 = [string1 stringByAppendingString:self.receive2];
        string1 = [string1 stringByAppendingString:string2];
        string1 = [string1 stringByAppendingString:self.receive];
        string1 = [string1 stringByAppendingString:string3];
        
        //解析网址
        NSURL *url = [NSURL URLWithString:string1];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *listArray = result[@"list"];
            [self.listMutArray removeAllObjects];
            for (NSDictionary *dict  in listArray) {
                ListDetailModel *model = [[ListDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [self.listMutArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
               
                [self.mainTableView reloadData ];
                
            });
        }];
    });
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
